﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace GroupProjectWinforms
{
    public partial class AddData : Form
    {
        public AddData()
        {
            InitializeComponent();
            autoGetTeamsData();
            autoGetMembersData();
            txtTeamNotes.MaxLength = 255;
            txtTeamName.MaxLength = 10;

            txtMemberName.MaxLength = 50;
            txtMemberNotes.MaxLength = 255;

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtTeamName.Text = "";
            txtGrade.Text = "0";
            txtTeamNotes.Text = "";
            txtGoalsFor.Text = "0";
            txtGoalsAgainst.Text = "0";
        }

        public void autoGetTeamsData()
        {
            try
            {
                string url = ConfigurationManager.ConnectionStrings["SqlProviderCon"].ToString();
                string sql = "select * from teams";
                using (SqlConnection con = new SqlConnection(url))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(sql, con))
                    {
                        DataSet ds = new DataSet();
                        da.Fill(ds);

                        dataGridViewTeam.DataSource = ds.Tables[0];
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(this, "Bug " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
            string url = ConfigurationManager.ConnectionStrings["SqlProviderCon"].ToString();
            string sql = "select * from teams";
            using (SqlConnection con = new SqlConnection(url))
            {
                using (SqlDataAdapter da = new SqlDataAdapter(sql, con))
                {
                    DataSet ds = new DataSet();
                    SqlCommandBuilder builder = new SqlCommandBuilder(da);
                    da.Fill(ds, "teams");

                    DataRow row;
                    row = ds.Tables["teams"].NewRow();

                    row["TeamID"] = ds.Tables["teams"].Rows.Count + 1;
                    row["TeamName"] = txtTeamName.Text.Trim();
                    row["Grade"] = int.Parse(txtGrade.Text.Trim());
                    row["GoalsFor"] = int.Parse(txtGoalsFor.Text.Trim());
                    row["GoalsAgainst"] = int.Parse(txtGoalsAgainst.Text.Trim());
                    row["Notes"] = txtTeamNotes.Text.Trim();

                    ds.Tables["Teams"].Rows.Add(row);

                    da.Update(ds, "teams");

                    MessageBox.Show("Add successfully!");
                }
            }
             }
            catch (Exception ex)
            {

                MessageBox.Show(this, "Bug " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                autoGetTeamsData();
            }
        }

        private void btnNewMember_Click(object sender, EventArgs e)
        {
            cboxTeamID.SelectedItem = null;
            txtMemberName.Text = "";
            txtGoals.Text = "0";
            txtYellowCards.Text = "0";
            txtRedCards.Text = "0";
            txtMemberNotes.Text = "";
            txtMemberID.Text = "";
        }

        public void autoGetMembersData()
        {
            try
            {
                string url = ConfigurationManager.ConnectionStrings["SqlProviderCon"].ToString();
                string sql = "select * from members";
                using (SqlConnection con = new SqlConnection(url))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(sql, con))
                    {
                        DataSet ds = new DataSet();
                        da.Fill(ds);

                        dataGridViewMember.DataSource = ds.Tables[0];
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(this, "Bug " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAddMember_Click(object sender, EventArgs e)
        {
            try
            {
                string url = ConfigurationManager.ConnectionStrings["SqlProviderCon"].ToString();
                string sql = "select * from Members";
                using (SqlConnection con = new SqlConnection(url))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(sql, con))
                    {
                        DataSet ds = new DataSet();
                        SqlCommandBuilder builder = new SqlCommandBuilder(da);
                        da.Fill(ds,"teams");

                        DataRow row;
                        row = ds.Tables["Members"].NewRow();

                        row["MemberID"] = txtMemberID.Text.Trim();
                        row["MemberName"] = txtMemberName.Text.Trim();
                        row["Goals"] = int.Parse(txtGoals.Text.Trim());
                        row["YellowCards"] = int.Parse(txtYellowCards.Text.Trim());
                        row["RedCards"] = int.Parse(txtRedCards.Text.Trim());
                        row["Notes"] = int.Parse(txtMemberNotes.Text.Trim());
                        row["TeamID"] = int.Parse(cboxTeamID.SelectedText);
                        

                        ds.Tables["Members"].Rows.Add(row);

                        da.Update(ds,"teams");

                        MessageBox.Show("Add member successfully!");
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(this, "Bug " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                autoGetMembersData();
            }
        }


    }
}
